package de.seminar.lego3D.fragments;

import de.seminar.lego3D.Constants;
import de.seminar.lego3D.Legobrick;
import de.seminar.lego3D.R;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Fragment that shows the lego bricks and provides buttons to send the chosen brick.
 * @author Christian Lander
 *
 */
public class LegoBricksFragment extends DetailsFragment {

	/** gridview containing the lego bricks **/
	private GridView brickView;

	/** array containing an object for each lego brick **/
	private Legobrick[] lego_bricks;

	/** the current lego brick and the current color **/
	private ImageView currentBrick, sittingConstr;

	/** animations of the lego figure **/
	private AnimationDrawable animStop, animSend;

	/**
	 * Create a new instance of LegoBricksFragment, initialized to show the view
	 * at 'index'.
	 * @param index - the index of this fragment
	 */
	public static LegoBricksFragment newInstance(int index) {
		LegoBricksFragment f = new LegoBricksFragment();

		// Supply index input as an argument.
		Bundle args = new Bundle();
		args.putInt("index", index);
		f.setArguments(args);

		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		// Inflate the layout for this fragment
		View v = inflater.inflate(R.layout.lego_bricks_fragment, container,
				false);

		brickView = (GridView) v.findViewById(R.id.lego_bricks);
		currentBrick = (ImageView) v.findViewById(R.id.current_lego);
		sittingConstr = (ImageView) v.findViewById(R.id.constrAnim);

		if (Constants.CURRENT_LEGO == null) {
			sittingConstr.setBackgroundResource(R.anim.constructor_stop_anim);
			animStop = (AnimationDrawable) sittingConstr.getBackground();
		} else {
			sittingConstr.setBackgroundResource(R.anim.constructor_send_anim);
			animSend = (AnimationDrawable) sittingConstr.getBackground();
		}

		if (Constants.CURRENT_LEGO != null) {
			currentBrick.setImageDrawable(Constants.CURRENT_LEGO
					.getDrawableIcon());
			currentBrick.setBackgroundColor(Constants.CURRENT_LEGO.color);
		}

		brickView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

				currentBrick.setImageDrawable(lego_bricks[position]
						.getDrawableIcon());
				currentBrick.setBackgroundColor(lego_bricks[position].color);

				Constants.CURRENT_LEGO = lego_bricks[position];

				sittingConstr
						.setBackgroundResource(R.anim.constructor_send_anim);
				animSend = (AnimationDrawable) sittingConstr.getBackground();

				sittingConstr.requestFocus();
			}
		});

		initBricks();

		return v;
	}

	/**
	 * starts an animation according to the boolean flag.
	 * 
	 * @param first
	 */
	public void startAnimation(boolean first) {
		if (first) {
			animStop.stop();
			animStop.start();
		} else {
			animSend.stop();
			animSend.start();
		}
	}
	
	@Override
	public void onStop() {
		
		if (animStop != null) {
			animStop.stop();
		}
		
		if (animSend != null) {
			animSend.stop();
		}
		
		super.onStop();
	}

	/**
	 * Initializes the array and fills the view.
	 */
	private void initBricks() {
		Context ctx = getActivity();
		lego_bricks = new Legobrick[7];
		lego_bricks[0] = new Legobrick(R.drawable.lego1, ctx, 1);
		lego_bricks[1] = new Legobrick(R.drawable.lego2, ctx, 1, 1);
		lego_bricks[2] = new Legobrick(R.drawable.lego3, ctx, 1, 1, 1);
		lego_bricks[3] = new Legobrick(R.drawable.lego4, ctx, 1, 1, 1, 1);
		lego_bricks[4] = new Legobrick(R.drawable.lego5, ctx, 2, 2, 2, 2);
		lego_bricks[5] = new Legobrick(R.drawable.lego6, ctx, 2, 2);
		lego_bricks[6] = new Legobrick(R.drawable.lego7, ctx, 2, 2, 2);
	//	lego_bricks[7] = new Legobrick(R.drawable.lego8, ctx, 1, 2);

		brickView.setAdapter(new ImageAdapter(ctx));
	}

	/**
	 * Adapter for the gridview containing the images of the lego bricks.
	 * 
	 * @author christian
	 * 
	 */
	public class ImageAdapter extends BaseAdapter {

		/**
		 * Creates a new adapter
		 * 
		 * @param c
		 */
		public ImageAdapter(Context c) {
		}

		/**
		 * Returns the number of items of the gridview
		 */
		public int getCount() {
			return lego_bricks.length;
		}

		/**
		 * Returns a object reference to an item at the position.
		 */
		public Object getItem(int position) {
			return lego_bricks[position];
		}

		/**
		 * Returns the item ID.
		 */
		public long getItemId(int position) {
			return position;
		}

		/**
		 * create a new ImageView for each item referenced by the Adapter
		 */
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = lego_bricks[position].getIcon();
			if (Constants.CURRENT_COLOR != -1) {
				lego_bricks[position].setColor(Constants.CURRENT_COLOR);
				lego_bricks[position].getIcon().setBackgroundColor(
						Constants.CURRENT_COLOR);
			}
			return v;
		}
	}

}
